﻿using MeterialManager.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MeterialManager.Model
{
    public class Meterial
    {
        public int Id { get; set; }
        public string CategoryCode { get; set; }
        public string MeterialCode { get; set; }
        public string MeterialName { get; set; }
        public string Unit { get; set; }
        public int Stocks { get; set; }
        public string Remark { get; set; }


        public static List<Meterial> ListAll()
        {
            List<Meterial> meterials = new List<Meterial>();
            DataTable dt = SqlHelper.ExecuteTable("SELECT * FROM Meterials");
            foreach (DataRow dr in dt.Rows)
            {
                meterials.Add(dr.DataRowToModel<Meterial>());
            }
            return meterials;
        }
        public static int Insert(Meterial meterial)
        {
            int rows = SqlHelper.ExecuteNonQuery("INSERT INTO Meterials(CategoryCode,MeterialCode,MeterialName,Unit,Stocks,Remark) VALUES(@CategoryCode,dbo.MeterialCode_fun(@CategoryCode),@MeterialName,@Unit,@Stocks,@Remark)",
                new SqlParameter("@CategoryCode", meterial.CategoryCode),
                new SqlParameter("@MeterialName", meterial.MeterialName),
                new SqlParameter("@Unit", meterial.Unit),
                new SqlParameter("@Stocks", meterial.Stocks),
                new SqlParameter("@Remark", meterial.Remark)
                );
            return rows;
        }

        public static int Update(Meterial meterial)
        {
            int rows = SqlHelper.ExecuteNonQuery("UPDATE Meterials SET CategoryCode=@CategoryCode,MeterialName=@MeterialName,Unit=@Unit,Stocks=@Stocks,Remark=@Remark WHERE Id=@Id",
                new SqlParameter("@Id", meterial.Id),
                new SqlParameter("@CategoryCode", meterial.CategoryCode),
                new SqlParameter("@MeterialName", meterial.MeterialName),
                new SqlParameter("@Unit", meterial.Unit),
                new SqlParameter("@Stocks", meterial.Stocks),
                new SqlParameter("@Remark", meterial.Remark)
                );
            return rows;
        }

        public static int Delete(int id)
        {
            int rows = SqlHelper.ExecuteNonQuery("DELETE FROM Meterials WHERE Id=@Id",
                new SqlParameter("@Id", id)
                );
            return rows;
        }
        public static bool Exists(string meterialCode)
        {
            List<Meterial> meterials = ListAll();
            return meterials.Find(m => m.MeterialCode == meterialCode) == null;
        }

        public static bool Exists(string meterialCode,int id)
        {
            List<Meterial> meterials = ListAll();
            return meterials.Find(m => m.MeterialCode == meterialCode &&  m.Id==id) != null || meterials.Find(m => m.MeterialCode == meterialCode) == null;
        }
    }
}
